// LGUIVSlider.cpp: implementation of the LGUIVSlider class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LGUIHSlider.h"
#include "LGUIContainer.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

LGUIHSlider::LGUIHSlider(int id, int _x, int _y, int _w, int _h, int _value, int _min, int _max, int _userdata, short16 _knobColor) :
  LGUIWindow(_x,_y,_w,_h)
{
  ID=id; 
  value=_value; 
  minValue=_min; 
  maxValue=_max;
  knobSize=9;
  dragging=false;
  knobColor=_knobColor;
  setUserData(_userdata);
}

LGUIHSlider::~LGUIHSlider()
{

}

void LGUIHSlider::penDown(int px, int py)
{
  if (isInside(px,py))
  {
    dragging=true;
    redraw();
  }
}


void LGUIHSlider::penUp(int px, int py)
{
  if (dragging)
    redraw();
  dragging=false;
}

void LGUIHSlider::penMove(int px, int py, bool pressed)
{
  if (!dragging) return;
  px -= (x+knobSize/2);
  value = (px)*(maxValue-minValue)/(w-knobSize-1);
  value += minValue;
  clamp();
  
  if (parent) 
    parent->deliverMessage(LGUIMessage(MSG_SLIDERCHANGED,ID,value,userData));
  
  redraw();
}

void LGUIHSlider::draw(int xoff, int yoff)
{
  if (isDirty)
  {
    buffer.rect(x+xoff,y+yoff,x+xoff+w,y+yoff+h,dragging ? COLOR(0x80ff80) : COLOR(0x404040));
    buffer.rectFill(x+xoff+1,y+yoff+1,x+xoff+w,y+yoff+h,COLOR(0x404040));
    buffer.hLine(x+xoff+1,y+yoff+1,x+xoff+w,COLOR(0x606060));
    buffer.hLine(x+xoff+1,y+yoff+h-1,x+xoff+w,COLOR(0x606060));
    int knobX = x+xoff+(w-knobSize-1)*(value-minValue)/(maxValue-minValue)+1;
    buffer.rectFillDarkFaded(knobX,y+yoff+1,knobX+knobSize,y+yoff+h,dragging ? whitenColor(knobColor) : knobColor);
    buffer.rect(knobX-1,y+yoff,knobX+knobSize,y+yoff+h,dragging ? COLOR(0x80ff80) : COLOR(0));
    LRect dirty = *this;
    dirty.x+=xoff;
    dirty.y+=yoff;
    dirty.w++;
    dirty.h++;
    buffer.addDirtyRect(dirty);
    isDirty = false;
  }
}